package cn.iocoder.yudao.module.dating.dal.mysql.smallNote;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.dating.controller.admin.smallNote.vo.SmallNotePageReqVO;
import cn.iocoder.yudao.module.dating.controller.app.samllNote.vo.AppSmallNoteTakeReqVO;
import cn.iocoder.yudao.module.dating.dal.dataobject.smallNote.SmallNoteDO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * 小纸条 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface SmallNoteMapper extends BaseMapperX<SmallNoteDO> {

    default PageResult<SmallNoteDO> selectPage(SmallNotePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SmallNoteDO>()
                .eqIfPresent(SmallNoteDO::getUserId, reqVO.getUserId())
                .eqIfPresent(SmallNoteDO::getSex, reqVO.getSex())
                .eqIfPresent(SmallNoteDO::getImages, reqVO.getImages())
                .eqIfPresent(SmallNoteDO::getAccount, reqVO.getAccount())
                .eqIfPresent(SmallNoteDO::getAccountImg, reqVO.getAccountImg())
                .eqIfPresent(SmallNoteDO::getDesp, reqVO.getDesp())
                .eqIfPresent(SmallNoteDO::getPubFlag, reqVO.getPubFlag())
                .eqIfPresent(SmallNoteDO::getHighFlag, reqVO.getHighFlag())
                .eqIfPresent(SmallNoteDO::getViews, reqVO.getViews())
                .eqIfPresent(SmallNoteDO::getExtractNum, reqVO.getExtractNum())
                .eqIfPresent(SmallNoteDO::getExtractDestroyNum, reqVO.getExtractDestroyNum())
                .eqIfPresent(SmallNoteDO::getDestroyFlag, reqVO.getDestroyFlag())
                .betweenIfPresent(SmallNoteDO::getAutoDestroyTime, reqVO.getAutoDestroyTime())
                .eqIfPresent(SmallNoteDO::getStatusFlag, reqVO.getStatusFlag())
                .betweenIfPresent(SmallNoteDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SmallNoteDO::getId));
    }

    default SmallNoteDO selectNote(AppSmallNoteTakeReqVO reqVO){
        LambdaQueryWrapperX<SmallNoteDO> wrapperX = new LambdaQueryWrapperX<SmallNoteDO>()
                .eq(StringUtils.hasText(reqVO.getProvince()) && !Objects.equals("0", reqVO.getProvince()),
                        SmallNoteDO::getProvince, reqVO.getProvince())
                .eq(StringUtils.hasText(reqVO.getCity()) && !Objects.equals("0", reqVO.getCity()),
                        SmallNoteDO::getCity, reqVO.getCity())
                .eq(StringUtils.hasText(reqVO.getArea()) && !Objects.equals("0", reqVO.getArea()),
                        SmallNoteDO::getArea, reqVO.getArea())
                .eq(StringUtils.hasText(reqVO.getSex()),
                        SmallNoteDO::getSex, reqVO.getSex())
                .eq(StringUtils.hasText(reqVO.getBoxType()),
                        SmallNoteDO::getBoxType, reqVO.getBoxType())
                .eq(SmallNoteDO::getDestroyFlag, false)
                .eq(SmallNoteDO::getStatusFlag, true);
//                .eq(SmallNoteDO::getDeleted, false);
        Long count = selectCount(wrapperX);
        if (count == 0L) {
            return null;
        }
        long offset = (long)(Math.random() * count);
        wrapperX.last("limit " + offset + ", 1");
        return selectOne(wrapperX);
    }
}